#include "guish.hpp"
#ifndef CLIENTCONNECTION_HPP
#define CLIENTCONNECTION_HPP
#include "IOManager.hpp"
#include "RPC.hpp"

class ClientConnection
	:public IOUser,
	 public RemoteHost
{
public:
	ClientConnection(int termInputFd, int termOutputFd);
	
	void prepareSelect(int priority, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, int *maxfd);
	void postSelect(int priority, fd_set *readfds, fd_set *writefds, fd_set *exceptfds);
	
	void setInteractiveShell(Shell *shell);
	
	void sendInitialization();
	void doExit();
	void hangUp();
	bool isConnected();
	bool isIdle();
	
	// Functions called remotely, by the terminal
	void runCommand(int job, string cmd, int termWidth, int termHeight);
	void windowResize(int job, int newWidth, int newHeight);
	void requestTabCompletion(string prefix, string suffix);
	
	void sendData(int len, const char *data);
	
protected:
	void pullFromTerminal();
	void pushToTerminal();
	void flushTerminal();
	
protected:
	Shell *shell;
	bool connected;
	int inFd;
	int outFd;
	StreamBuffer terminalOutput;
};

extern ClientConnection *clientConnection;

#endif
